package com.skb.core.dao;

import com.skb.core.model.Segment;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

@Repository("segmentDao")
public class SegmentDaoImpl implements SegmentDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Segment create(Segment segment) {
        sessionFactory.getCurrentSession().persist(segment);
        return segment;
    }

    @Override
    public Segment get(Serializable id) {
        return (Segment) sessionFactory.getCurrentSession().get(Segment.class, id);
    }

    @Override
    public Segment update(Segment segment) {
        sessionFactory.getCurrentSession().update(segment);
        return segment;
    }

    @Override
    public void delete(Segment segment) {
        sessionFactory.getCurrentSession().delete(segment);
    }

    @Override
    public List<Segment> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from Segment").list();
    }
}
